//
//  AlbumCoverCell.m
//  OntheRoadV4
//
//  Created by ZhangYanhua on 14/11/26.
//  Copyright (c) 2014年 EricHuang. All rights reserved.
//

#import "AlbumCoverCell.h"
#import "QWImageMangerConfig.h"
static void *GroupThumbnailObserverContext = &GroupThumbnailObserverContext;

@interface AlbumCoverCell()
{
}

@property (nonatomic, strong) MyAssetsGroup *curGroup;

@end

@implementation AlbumCoverCell

- (void)awakeFromNib
{

    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.albumCheckedImg.hidden = YES;
    self.albumTitle.textColor = BUTTON_TEXT_COLOR_DARK;
    self.albumPhotoCntLabel.textColor = LABEL_TEXT_COLOR_GRAY;
    self.albumCheckedImg.image = [QWImageMangerConfig imageNamed:@"icon_check"];
    [self.thumbnailView.layer setCornerRadius:2];
    [self.thumbnailView setClipsToBounds:YES];
    
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated
{
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

+ (CGFloat)getCellHeight
{
    return 60.0f;
}

- (void)updateCellWithGroup:(MyAssetsGroup *)group showPhotos:(BOOL)showPhotos isLastCell:(BOOL)isLast
{
    self.curGroup = group;

    NSString *name = [group groupName];
    NSInteger count = showPhotos ? [group numberOfPhotos] : [group numberOfVideos];
    
    [self.thumbnailView setImage:[group groupThumbnail]];
    
    self.albumTitle.text = name;

    if (count > 0) {
        self.albumPhotoCntLabel.text = [NSString stringWithFormat:@"%ld", (long)count];
    } else {
        self.albumPhotoCntLabel.text = nil;
    }

    self.botSepLien.hidden = isLast;
}

- (void)updateSelectionState:(BOOL)isSelected
{
    if (isSelected) {
        self.albumCheckedImg.hidden = NO;

        self.albumTitle.textColor = [[QWImageMangerConfig shareInsatnce] mainFontBlueColor];
        self.albumPhotoCntLabel.textColor = [[QWImageMangerConfig shareInsatnce] mainFontBlueColor];
    } else {
        self.albumCheckedImg.hidden = YES;
        self.albumTitle.textColor = LABEL_TEXT_COLOR_GRAY;
        self.albumPhotoCntLabel.textColor = LABEL_TEXT_COLOR_GRAY;
    }
}

#pragma mark -
#pragma mark KVO for thumbnail image

- (void)dealloc
{
    if (_curGroup) {
        [_curGroup removeObserver:self forKeyPath:@"groupThumbnail" context:GroupThumbnailObserverContext];
    }
}

- (void)setCurGroup:(MyAssetsGroup *)newcurGroup
{
    if (_curGroup){
        if(_curGroup) [_curGroup removeObserver:self forKeyPath:@"groupThumbnail" context:GroupThumbnailObserverContext];
        _curGroup = nil;
    }
    if (newcurGroup){
        _curGroup = newcurGroup;
        if (_curGroup) [_curGroup addObserver:self forKeyPath:@"groupThumbnail" options:NSKeyValueObservingOptionNew context:GroupThumbnailObserverContext];
    }
    
}

- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
    if (context == GroupThumbnailObserverContext) {
        [self.thumbnailView setImage:[self.curGroup groupThumbnail]];
    }
}

@end
